<?php
 /**
 * PHP Entity Converter.
 * @author We Tell <hello@wetell.co.nz>
 * @link http://www.wetell.co.nz
 * @version: 1
 *
 * @license GNU General Public License v3.0
 */

$content = stripslashes($_POST['textconvert']);
//create class instance
$oUnicodeReplace = new unicode_replace_entities();
//set if safe punc is being used
$oUnicodeReplace->setSafePunc($_POST['safepunctuation']);
//run the class
$unicontent = $oUnicodeReplace->UTF8entities($content);
//echo back the results
$replaced = preg_replace("/(&#)+/","&amp;#",$unicontent);
echo "<br /><strong>Your text converted to HTML (make sure this looks correct):</strong><br />".$unicontent."<br /><br /><strong>Copy the HTML from here:</strong><br />".$replaced;

//this is a mix of code from php.net - http://www.php.net/manual/en/function.htmlentities.php#92105
//simple task: convert everything from utf-8 into an NCR[numeric character reference]
class unicode_replace_entities {
	private $safePunc = false;
	//set safe punctation var
	//safe punc swaps out some results for email client safe results
	public function setSafePunc($string){
		if($string == 'yes') {
			$this->safePunc = true;
		}
	}


	public function UTF8entities($content="") {
		$contents = $this->unicode_string_to_array($content);
		$swap = "";
		$iCount = count($contents);
		for ($o=0;$o<$iCount;$o++) {
			$contents[$o] = $this->unicode_entity_replace($contents[$o]);
			$swap .= $contents[$o];
		}
		return mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not.
	}

	public function unicode_string_to_array( $string ) { //adjwilli
		$strlen = mb_strlen($string);
		while ($strlen) {
			$array[] = mb_substr( $string, 0, 1, "UTF-8" );
			$string = mb_substr( $string, 1, $strlen, "UTF-8" );
			$strlen = mb_strlen( $string );
		}
		return $array;
	}

	public function unicode_entity_replace($c) { //m. perez
		$h = ord($c);

		//simply return chars that don't need unicode equivalents
		if ($h >= 97 && $h <= 122 ) {
			return $c;
		}

		if ($h >= 65 && $h <= 90 ) {
			return $c;
		}

		if ($h >= 48 && $h <= 57 ) {
			return $c;
		}

		if ($h <= 32) {
			return $c;
		}

		if ($h  <= 127 ) {
			$h = "&#" . $h . ";";
			return $h;
		}


		//catch all other characters
		if ($h <= 0xDF) {
			$h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
			return $this->uniChar($h);
		} else if ($h <= 0xEF) {
				$h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
				return $this->uniChar($h);
			} else if ($h <= 0xF4) {
				$h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
				return $this->uniChar($h);
			}
	}


	private function uniChar($charCode) {
		//check if we are using safe characters for email clients.
		if($this->safePunc){
			switch ($charCode) {
			case 8220:
				$charCode = 34;
				break;
			case 8221:
				$charCode = 34;
				break;
			case 8216:
				$charCode = 39;
				break;
			case 8217:
				$charCode = 39;
				break;
			case 8211:
				$charCode = 45;
				break;
			case 8212:
				$charCode = 45;
				break;
			}

		}

		$a = "&#" . $charCode . ";";
		return $a;
	}
}//end class

?>